package com.jiaai.common.api.sms;
//接口类型：互亿无线触发短信接口，支持发送验证码短信、订单通知短信等。
// 账户注册：请通过该地址开通账户http://sms.ihuyi.com/register.html
// 注意事项：
//（1）调试期间，请用默认的模板进行测试，默认模板详见接口文档；
//（2）请使用APIID（查看APIID请登录用户中心->验证码短信->产品总览->APIID）及 APIkey来调用接口；
//（3）该代码仅供接入互亿无线短信接口参考使用，客户可根据实际需要自行编写；

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


public class SmsValidationUtil1 {

	public static void main(String[] args) {
		init("dgja", "832263");
		try {
			sendCode("13712306274", generateCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String Url = "http://sms.ue35.net/sms/interface";
	//http://106.ihuyi.cn/webservice/sms.php?method=Submit
	//
	public static String PHONE ;
	public static String PWD ;

	public static void init(String ph, String pw){
		SmsValidationUtil1.PHONE = ph;
		SmsValidationUtil1.PWD = pw;
	}

	public static String sendCode(String phone, String code) throws Exception {
		String sendUrl=Url+"/sendmess.htm?";
		String result="";
		try{
			String postdata[]={
					"username="+URLEncoder.encode(PHONE,"UTF-8"),
					"userpwd="+URLEncoder.encode(PWD,"UTF-8"),
					"mobiles="+URLEncoder.encode(phone,"UTF-8"),
					"content="+URLEncoder.encode("您的验证码是：".concat(code).concat("。请不要把验证码泄露给其他人。"),"UTF-8"),
			};
			result=http(sendUrl,implode("&",postdata));
		}catch(UnsupportedEncodingException e){
			e.printStackTrace();
		}
		return result;

	}

	public static String generateCode(){
		return String.valueOf((int) (100000 + (Math.random()* 900000)));
	}

	/**
	 * HTTP 操作类（POST提交）
	 * @param   postdata   POST提交的数据
	 */
	public static String http(String url, String postdata){
		PrintWriter out = null;
		BufferedReader br = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			URLConnection conn = realUrl.openConnection();
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			out = new PrintWriter(conn.getOutputStream());
			out.print(postdata);
			out.flush();
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {

				result += line;
			}
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try{
				if(out!=null){
					out.close();
				}
				if(br!=null){
					br.close();
				}
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
		}
		return result;
	}
	/**
	 * 合并String数组为字符串
	 * @param   fg   分隔符
	 * @param   phone   存储手机号的字符数组
	 */
	public static String implode(String fg, String[] phone){
		StringBuffer sb = new StringBuffer();
		sb. append(phone[0]);
		for(int i = 1; i < phone.length; i++){
			sb. append(fg+phone[i]);
		}
		return sb.toString();
	}

}